package com.gougou.controller;

import com.alibaba.druid.filter.AutoLoad;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gougou.pojo.TbItem;
import com.gougou.pojo.TbItemCat;
import com.gougou.pojo.TbOrder;
import com.gougou.pojo.TbUser;
import com.gougou.service.UserService;
import com.gougou.service.impl.MybatisServiceImpl;
import com.gougou.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by EX-ZHANZHENQIANG001 on 2018-05-03.
 */
@RequestMapping("/cache")
@Controller
public class CacheController {


    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private MybatisServiceImpl mybatisService;

    @RequestMapping("/pageAdd")
    @ResponseBody
    public Object pageAdd() {
        Map<String, String> param = new HashMap<>();
        param.put("p1", "1");
        param.put("p2", "5");
        param.put("p3", null);
        String result = userService.pageAdd(param);
        System.out.println(result);
        return param;
    }


    @RequestMapping("/getUser")
    @ResponseBody
    public Object getUser() {
        TbUser result = userService.getUserbyName("admin");
        System.out.println(result);
        return result;
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public Object addUser(@RequestParam TbUser tbUser) {
        try {
            userService.registerUser(tbUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }


    @RequestMapping("/queryItem")
    @ResponseBody
    public Object queryItem(HttpServletRequest request) {
        try {
            Map<String, String> param = new HashMap<String, String>();
            param.put("title", request.getParameter("title"));
            param.put("price", request.getParameter("price"));
            List<TbItem> tbItemList = mybatisService.queryItem(param);
            return tbItemList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "fail";
    }



    @RequestMapping("/queryItemByPage")
    @ResponseBody
    public Object queryItemByPage(HttpServletRequest request) {
        try {
            Integer pageNum = request.getParameter("pageNum")!=null? Integer.valueOf(request.getParameter("pageNum")) :1;
            Integer pageSize = request.getParameter("pageSize")!=null? Integer.valueOf(request.getParameter("pageSize")):10;
            PageHelper.startPage(pageNum, pageSize, true);
            Map<String, String> param = new HashMap<String, String>();
            param.put("title", request.getParameter("title"));
            param.put("price", request.getParameter("price"));
            List<TbItem> tbItemList = mybatisService.queryItem(param);
            PageInfo<TbItem> pageInfo = new PageInfo<>(tbItemList);
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "fail";
    }


    @RequestMapping("/queryItemCat")
    @ResponseBody
    public Object queryItemCat(HttpServletRequest request) {
        try {
            List list = null;
            String names = request.getParameter("names");
            if(!StringUtils.isEmpty(names)) {
                list = Arrays.asList(names.split(","));
            }
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("id", request.getParameter("id"));
            param.put("names", list);
            List<TbItemCat> tbItemList = mybatisService.queryItemCat(param);
            return tbItemList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "fail";
    }


    @RequestMapping("/getItemCatChild")
    @ResponseBody
    public Object getItemCatChild(HttpServletRequest request,
                                  @RequestParam String rootId,
                                  @RequestParam String cName) {
        try {
            List list = null;
            Map<String, String> param = new HashMap<String, String>();
            param.put("rootId", rootId);
            param.put("cName", cName);
            List resultList = mybatisService.getItemCatChild(param);
            return resultList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "fail";
    }

}
